package com.hnu.store.controller;

import com.hnu.store.dto.NftDTO;
import com.hnu.store.entity.Work;
import com.hnu.store.service.IWorkService;
import com.hnu.store.utils.JsonResult;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * work的controller
 */
@RestController
@CrossOrigin

@RequestMapping("work")
public class WorkController extends BaseController {

  @Autowired
  private IWorkService workService;

  /**
   * 添加nft
   * @param multipartFile 上传文件
   * @param work nft信息
   * @return json
   */
  @RequestMapping("addNft")
  public JsonResult addNft(MultipartFile multipartFile, Work work) {
    Work data = workService.addNft(multipartFile, work);
    //  BaseController没有捕获异常
    JsonResult result = new JsonResult(OK);
    result.addData("nft", data);
    return result;
  }

  /**
   * 预览数据nftList
   * @param userId 用户id
   * @param stringPara 关键字
   * @param status 状态
   * @param classification 分类
   * @return nft列表
   */
  @RequestMapping("getNftInfo")
  public JsonResult getNftInfo(Integer userId, String stringPara, String status, String classification) {
    List<NftDTO> nftList = workService.getNftList(userId,stringPara,status,classification);

    JsonResult result = new JsonResult(OK);
    result.addData("nftList", nftList);
    return result;
  }

  /**
   * 根据nftId获取nft
   * @param workId nft的id
   * @return nft信息
   */
  @RequestMapping("getNft")
  public JsonResult getNft(String workId) {
    Work work = workService.getWorkById(workId);
    //  没有抛出异常
    JsonResult result = new JsonResult(OK);
    result.addData("nftInfo", work);
    return result;
  }

  /**
   * 删除nft
   * @param id nft的id
   * @return json
   */
  @RequestMapping("deleteNft")
  public JsonResult deleteNft(String id) {
    workService.deleteNft(id);
    //  没有抛出异常，删除成功
    return new JsonResult(OK);
  }

  /**
   * 获取所有NFT
   * @return 获取全部nft
   */
  @RequestMapping("getAllNft")
  public JsonResult getAllNft(){
    List<NftDTO> workList=workService.getAllNft();
    //异常
    JsonResult result = new JsonResult(OK);
    result.addData("allNft",workList);
    return result;
  }

  /**
   * 编辑nft
   * @param multipartFile 上传文件
   * @param work 新的nft信息
   * @return json
   */
  @RequestMapping("editNft")
  public JsonResult editNft(MultipartFile multipartFile,Work work) {
    Work data = workService.editNft(multipartFile,work);

    //  没有抛出异常，删除成功
    JsonResult jsonResult = new JsonResult(OK);
    jsonResult.addData("newWork",data);
    return jsonResult;
  }

  /**
   * 下载nft作品
   * @param filePath 文件路径
   * @param fileName 文件名称
   * @param response http的response
   */
  @RequestMapping("downloadNft")
  public void downloadNft(String filePath,String fileName, HttpServletResponse response) {
    workService.downloadNft(filePath,fileName,response);
  }

  /**
   * nft拆分上传
   * @param multipartFile 文件
   * @param work nft
   * @param count 拆分份数
   * @throws IOException IO错误
   */
  @RequestMapping("addNftPieces")
  public JsonResult addNftPieces(MultipartFile multipartFile,int count,Work work)
      throws IOException {
    switch (work.getType()) {
      //  文字作品
      case 2:
        workService.addTextPieces(multipartFile,work,count);
        break;
      //  视频作品
      case 3:
        break;
      //  音频作品
      case 4:
        workService.addAudioPieces(multipartFile,work,count);
        break;
      //  摄影作品，美术作品
      case 1:
      case 5:
        if(count==4) {
          workService.addPicturePieces(multipartFile,work,2,2);
        } else if (count==9) {
          workService.addPicturePieces(multipartFile,work,3,3);
        }
        break;
      default:
        System.out.println("请设置正确的作品类别");
    }
    return new JsonResult(OK);
  }

  /**
   * 预览
   * @param multipartFile 文件
   * @return 返回预览图片
   */
  @RequestMapping("preview")
  public JsonResult preview(MultipartFile multipartFile) {
    String data = workService.preview(multipartFile);
    JsonResult result = new JsonResult(OK);
    result.addData("previewUrl",data);
    return result;
  }

  /**
   * 取消预览，删除临时文件
   * @param multipartFile 文件
   * @return 返回json
   */
  @RequestMapping("cancelPreview")
  public JsonResult cancelPreview(MultipartFile multipartFile) {
    workService.cancelPreview(multipartFile);
    return new JsonResult(OK);
  }

  /**
   * 上链生成存证
   * @param id workid
   * @param chainHash 链上哈希
   * @param status 状态变成1表示已上链
   * @param blockHeight 区块高度
   * @param chainTime 上链时间
   * @return json
   */
  @RequestMapping("uploadChain")
  public JsonResult uploadChain(String id, String chainHash, Integer status, String blockHeight,
      String chainTime) {
    workService.uploadChain(id,chainHash,status,blockHeight,chainTime);
    return new JsonResult(OK);
  }

  @RequestMapping("viewDetails")
  public JsonResult viewDetails(String workId,String chainTime,String blockHeight) {
    workService.viewDetails(workId,chainTime,blockHeight);
    return new JsonResult(OK);
  }
}
